import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, ListData, DealerSimpleList, DealerList, Detail} from './DTO';
const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询发运单信息'
});

const shipmentDesc = formatMessage({
    id: 'api.shipmentDesc',
    defaultMessage: '订单出运信息'
});

const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/partShipment/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));
// 查询备件
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partShipments?${queryString}`, null, ListData)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/partShipments/${id}`, null, Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
// 发运
export const shipmentData = (id, bodyArgs) => request(`${baseUrl}/partShipments/${id}/shipment`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(bodyArgs)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, shipmentDesc));
// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer/simple?${queryString}`, null, DealerSimpleList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
